package com.android.yummycookies;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.view.animation.BounceInterpolator;
import android.widget.Toast;

import com.android.yummycookies.asyncTask.LoadBitmapThread;
import com.android.yummycookies.asyncTask.LoadCookieTypesThread;
import com.android.yummycookies.asyncTask.ScaleBitmapThread;
import com.android.yummycookies.component.loadingScreen.SimpleThemeLoadingScreen;
import com.android.yummycookies.model.theme.Theme;
import com.android.yummycookies.service.storer.imagesStorer.BitmapsStoreService;
import com.android.yummycookies.service.storer.imagesStorer.binder.BitmapsStoreBinder;
import com.android.yummycookies.types.CookieType;
import com.android.yummycookies.util.Logger;
import com.android.yummycookies.util.Validator;

public class ThemeLoadingActivity extends Activity {

	public static final String TAG = ThemeLoadingActivity.class.getName();

	public static final String THEME_TO_LOAD = "THEME_TO_LOAD";
	public static final String HEIGHT_TO_SCALE = "HEIGHT_TO_SCALE";
	public static final String WIDTH_TO_SCALE = "WIDTH_TO_SCALE";
	public static final String ACTIVITY_TO_START = "ACTIVITY_TO_START";

	private BitmapsStoreBinder bitmapStore;
	private boolean serviceBounded;

	private SimpleThemeLoadingScreen loadingScreen;

	private Theme themeToLoad;
	private int heightToScale;
	private int widthToScale;

	private Intent activityToStart;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// recuperation des donnees
		Bundle bundle = getIntent().getExtras();

		themeToLoad = bundle.getParcelable(THEME_TO_LOAD);
		heightToScale = bundle.getInt(HEIGHT_TO_SCALE);
		widthToScale = bundle.getInt(WIDTH_TO_SCALE);

		Validator.checkIsPositive(TAG, "cookie's height to scale",
				heightToScale);
		Validator.checkIsPositive(TAG, "cookie's width to scale", widthToScale);

		activityToStart = bundle.getParcelable(ACTIVITY_TO_START);

		loadingScreen = createLoadingScreen();
		setContentView(loadingScreen);

		// bind le service de bitmapStorage
		Intent serviceIntent = new Intent(getBaseContext(),
				BitmapsStoreService.class);
		bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
	}

	@Override
	protected void onDestroy() {
		serviceBounded = false;
		unbindService(serviceConnection);
		super.onDestroy();
	}

	private ServiceConnection serviceConnection = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName className, IBinder service) {

			bitmapStore = (BitmapsStoreBinder) service;
			bitmapStore.getObserversPool().addObserver(handler);
			serviceBounded = true;

			// demarre le chargement des bitmaps
			loadBitmaps();
		}

		@Override
		public void onServiceDisconnected(ComponentName arg0) {
			serviceBounded = false;
		}
	};

	private Handler handler = new Handler(Looper.getMainLooper()) {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case LoadBitmapThread.END_LOADING_BITMAPS:
				loadCookieTypes();
				break;
			case LoadCookieTypesThread.END_LOADING_COOKIE_TYPES:
				fillCookyType();
				startIntent();
				break;
			default:
				Toast.makeText(getBaseContext(), "" + msg.what, 1000).show();
				// TODO gestion des erreurs en quittant l'application
				break;
			}

		}

		

	};

	private SimpleThemeLoadingScreen createLoadingScreen() {
		return new SimpleThemeLoadingScreen(this, themeToLoad, heightToScale,
				widthToScale);
	}

	private void loadBitmaps() {
		bitmapStore.loadAllBitmaps(themeToLoad.getBitmapsId());
	}
	
	private void loadCookieTypes() {
		bitmapStore.addCookieTypes(heightToScale, widthToScale, themeToLoad.getElemTypes());
	}

	

	private void fillCookyType() {
		for (CookieType cookieType : themeToLoad.getElemTypes()) {
			cookieType.setBitmap(bitmapStore.getScaledBitmap(cookieType
					.getBitmapId()));
			cookieType.setDestructionBitmap(bitmapStore
					.getScaledBitmap(cookieType.getDestructionBitmapId()));
		}

	}

	private void startIntent() {
		bitmapStore.getObserversPool().removeObserver(handler);

		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		startActivity(activityToStart);
		finish();
	};

}
